<?php

declare (strict_types=1);

namespace App\Model;

use App\Service\MomentsReviewService;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;

/**
 */
class MomentsReview extends Model
{
    public $timestamps = false;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'fly_moments_reviews';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    /**
     * 评论用户
     * @return HasOne
     */
    public function with_reviewer()
    {
        return $this->hasOne(AppUser::class, 'id', 'reviewer')
            ->select(['id', 'nickname', 'avatar']);
    }

    /**
     * 被回复用户
     * @return HasOne
     */
    public function with_replay_user()
    {
        return $this->hasOne(AppUser::class, 'id', 'replay_user')
            ->select(['id', 'nickname', 'avatar']);
    }

    /**
     * 获取点赞用户
     * @return HasMany
     */
    public function with_hearts()
    {
        return $this->hasMany(self::class, 'replay_to', 'id')
            ->where('delete_time', '=', 0)
            ->where('review_type', '=', MomentsReviewService::TYPE_ZAN)
            ->with(['with_reviewer'])
            ->select(['id', 'reviewer', 'replay_to', 'moments_id'])
            ->orderBy('heart_num', 'desc')
            ->orderBy('replay_time', 'asc');
    }


    /**
     * 子集评论
     * @return HasMany
     */
    public function with_sub_review()
    {
        return $this->hasMany(self::class, 'main_review_id', 'id')
            ->where('delete_time', '=', 0)
            ->where('review_type', '=', MomentsReviewService::TYPE_REVIEW_REVIEW)
            ->with(['with_reviewer', 'with_replay_user', 'with_hearts'])
            ->orderBy('heart_num', 'desc')
            ->orderBy('replay_time', 'asc');
    }


}